//
//  AdviceViewController.m
//  zhijian
//
//  Created by lcb on 2019/1/19.
//  Copyright © 2019 lcb. All rights reserved.
//

#import "AdviceViewController.h"
#import "Base.h"
#import "LTPlaceholderTextView.h"
#import "LTLimitTextField.h"
@interface AdviceViewController ()<UITextViewDelegate,UITextFieldDelegate>
@property (nonatomic, strong) UIView * feedView;
@property (nonatomic, strong) LTPlaceholderTextView * textView;
@property (nonatomic, strong) UITextField *phoneField;
@property (nonatomic, strong) UIButton * sendButton;
//字数的限制
@property (nonatomic, strong)UILabel *wordCountLabel;
@end

@implementation AdviceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self hyb_navWithLeftImage:@"back" title:@"问题反馈" leftClicked:^(UIButton *sender) {
        [self.navigationController popViewControllerAnimated:YES];
    }];
    // Do any additional setup after loading the view.
    [self initUI];
}
-(void)initUI
{
    self.feedView = [[UIView alloc]init];
    _feedView.backgroundColor = [UIColor whiteColor];
    _feedView.frame = CGRectMake(20, 84+[BaseShare statusBarHeight], SCREEN_WIDTH- 40, 220);
    [self.view addSubview:_feedView];
    //
    self.textView.frame =CGRectMake(0, 0, _feedView.frame.size.width, 100);
    [_feedView addSubview:self.textView];
    //
    self.wordCountLabel = [[UILabel alloc]initWithFrame:CGRectMake(_feedView.frame.size.width-75,  self.textView.frame.size.height -22, 70, 20)];
    _wordCountLabel.font = [UIFont systemFontOfSize:14.f];
    _wordCountLabel.textColor = [UIColor lightGrayColor];
    self.wordCountLabel.text = @"0/300";
    self.wordCountLabel.backgroundColor = [UIColor whiteColor];
    self.wordCountLabel.textAlignment = NSTextAlignmentRight;
    [_feedView addSubview:_wordCountLabel];
    //
    self.sendButton.frame = CGRectMake(0,self.textView.frame.origin.y+self.textView.frame.size.height+10, _feedView.frame.size.width, 40);
    [_feedView addSubview:self.sendButton];
    
}
-(LTPlaceholderTextView *)textView{
    
    if (!_textView) {
        _textView = [[LTPlaceholderTextView alloc]initWithFrame:CGRectZero];
        _textView.backgroundColor = [UIColor whiteColor];
        _textView.delegate = self;
        _textView.font = [UIFont systemFontOfSize:14.f];
        _textView.textColor = [UIColor blackColor];
        _textView.textAlignment = NSTextAlignmentLeft;
        _textView.editable = YES;
        _textView.layer.cornerRadius = 4.0f;
        _textView.layer.borderColor = [UIColor grayColor].CGColor;
        _textView.layer.borderWidth = 0.5;
        _textView.placeholder = @"写下你遇到的问题，或告诉我们你的宝贵意见~";
        _textView.textLimitType =LTTextLimitTypeLength;
        _textView.textLimitSize=300;
        _textView.maxSize =300;
    }
    
    return _textView;
}
-(UITextField*)phoneField
{
    if (!_phoneField) {
        _phoneField = [[UITextField alloc] init];
        _phoneField.textColor = [UIColor blackColor];
        _phoneField.clearsOnBeginEditing = YES;       // 当第二次输入的时候，清空上一次的内
        _phoneField.placeholder = @"联系方式";
        _phoneField.font = [UIFont boldSystemFontOfSize:14.0f];
        _phoneField.textAlignment = NSTextAlignmentLeft;
        _phoneField.borderStyle = UITextBorderStyleRoundedRect;// 设置输入框的样式
        _phoneField.minimumFontSize = 20.0f;// 设置自动缩小显示的最小字体大小
        _phoneField.returnKeyType = UIReturnKeyDefault;// 设置弹出的键盘带形式与带的按键
        _phoneField.keyboardType = UIKeyboardTypePhonePad;
        _phoneField.delegate = self;
    }
    return _phoneField;
}
- (UIButton *)sendButton{
    
    if (!_sendButton) {
        _sendButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _sendButton.layer.cornerRadius =20;
        _sendButton.layer.masksToBounds = YES;
        _sendButton.backgroundColor=navColor;
        [_sendButton setTitle:@"提交" forState:UIControlStateNormal];
        [_sendButton addTarget:self action:@selector(sendFeedBack) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _sendButton;
    
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [_textView resignFirstResponder];
    [_phoneField resignFirstResponder];
}

//在这个地方计算输入的字数
- (void)textViewDidChange:(UITextView *)textView
{
    NSInteger wordCount = textView.text.length;
    self.wordCountLabel.text = [NSString stringWithFormat:@"%ld/300",(long)wordCount];
}
//按下Done按钮的调用方法，我们让键盘消失

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    [textField resignFirstResponder];
    return YES;
    
}
-(void)sendFeedBack
{
    if (self.textView.text.length==0) {
        [BaseToast showAtTop:@"请输入内容！"];
        return;
    }
   __weak __typeof(self)weakSelf = self;
    [[Api sharedApi] sendFeedBack:self.textView.text callback:^(ApiResponse *response,NSString* errorMsg) {
        weakSelf.textView.text=@"";
        [BaseToast showAtTop:response.message];
    }];
}
@end
